public class LC_19 {
    public ListNode removeNthFromEnd(ListNode head, int n) {
            int lenth = 0;
            ListNode node = head;
            while(node!= null){
                lenth++;
                node = node.next;
            }
            int key = lenth - n - 1;
            ListNode delnode = new ListNode(0);
            delnode.next = head;
            head = delnode;
            while(key >= 0){
                delnode = delnode.next;
                key--;
            }
            if(delnode.next != null){
                delnode.next = delnode.next.next;
            }else {
                delnode.next = null;
            }
            return head.next;
        }
}

